import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/res/colors.dart';

import '../util/flat_button.dart';




typedef void CallBackWithResult(String result);

class InputTextPage extends StatefulWidget {
  InputTextPage({
    Key? key,
    required this.title,
    this.content = "",
    this.hintText = "",
    this.onPress,
    this.keyboardType = TextInputType.text,
  }) : super(key: key);

  final String title;
  final String content;
  final String hintText;
  final TextInputType keyboardType;
  final CallBackWithResult? onPress;

  @override
  _InputTextPageState createState() => _InputTextPageState();
}

class _InputTextPageState extends State<InputTextPage> {
  TextEditingController _controller = TextEditingController();
  FocusNode _contentFocusNode = FocusNode();
  @override
  void initState() {
    super.initState();
    _controller.text = widget.content;
  }

  Widget rightButton(){
    return FlatsButton(
      child: Text('完成',style: TextStyle(color: Colours.app_main,fontSize: 16),),
      onPressed: (){
        _contentFocusNode.unfocus();
        if(widget.onPress!=null){
          widget.onPress!(_controller.text);
        }
        Navigator.pop(context);
      },
    );
  }
  @override
  void dispose() {
    _contentFocusNode.unfocus();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.5,
        title: new Text(widget.title),
        actions: <Widget>[
          rightButton(),
        ],
      ),
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.white,
      body: Padding(
        padding: const EdgeInsets.only(
            top: 21.0, left: 16.0, right: 16.0, bottom: 16.0),
        child: TextField(
            maxLength: 30,
            maxLines: 5,
            autofocus: true,
            controller: _controller,
            focusNode: _contentFocusNode,
            keyboardType: widget.keyboardType,
            style: TextStyle(color: Colours.text_dark, fontSize: 14),
            decoration: InputDecoration(
                hintText: widget.hintText,
                border: InputBorder.none,
                fillColor: Colours.bg_gray,
                filled: true,
                hintStyle: TextStyle(color: Colours.text_dark, fontSize: 14))),
      ),
    );
  }
}
